package com.dnw.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangyu on 2017/12/6.
 */

public class RecommendProgramUtil {
    public static final int UNDOWNLOAD = 0;
    public static final int DOWNLOAD   = 1;
    public static final int INSTALLED  = 2;

    public static int checkProgramStatus(Context context, String appName) {
        File appFile = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "xiudou_app" + File.separator + appName + ".apk");
        if (isAppInstalled(context, appName)) {
            return INSTALLED;
        } else if (appFile.exists()) {
            return DOWNLOAD;
        } else {
            return UNDOWNLOAD;
        }

    }

    public static boolean isAppInstalled(Context context, String appName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        List<String> pName = new ArrayList<String>();
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = (String) pinfo.get(i).applicationInfo.loadLabel(packageManager);
                pName.add(pn);
            }
        }
        return pName.contains(appName);
    }

    public static void startApplicationWithName(Context context, String appName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                if (appName.equals((String) pinfo.get(i).applicationInfo.loadLabel(packageManager))) {
                    Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
                    resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    resolveIntent.setPackage(pinfo.get(i).packageName);
                    // 通过getPackageManager()的queryIntentActivities方法遍历
                    List<ResolveInfo> resolveinfoList = context.getPackageManager()
                            .queryIntentActivities(resolveIntent, 0);

                    ResolveInfo resolveinfo = resolveinfoList.iterator().next();
                    if (resolveinfo != null) {
                        // packagename = 参数packname
                        String packageName = resolveinfo.activityInfo.packageName;
                        // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
                        String className = resolveinfo.activityInfo.name;
                        // LAUNCHER Intent
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);

                        // 设置ComponentName参数1:packagename参数2:MainActivity路径
                        ComponentName cn = new ComponentName(packageName, className);

                        intent.setComponent(cn);
                        context.startActivity(intent);
                    }
                    break;
                }
            }
        }
    }

}
